package net.orange.framework.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import net.orange.architecture.process.ProcessFactory;
import net.orange.architecture.web.BaseController;
import net.orange.framework.logic.ICommoninfoProcess;
import net.orange.framework.model.Commoninfo;
import net.orange.framework.model.CommoninfoType;

/**
 * The common info managed bean using in JSF MVC modal. it is include the web
 * page content and control the action and page forward direction in the web
 * site.
 */
@ManagedBean(name="commoninfo")
@SessionScoped
public class CommoninfoController extends BaseController {

	/**
	 * The type want to search.
	 */
	private String s_type;

	/**
	 * The code want to search.
	 */
	private String sr_code;

	/**
	 * The value want to search.
	 */
	private String sr_value;

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.web.BaseController#getClazz()
	 */
	protected Class getClazz() {
		return Commoninfo.class;
	}

	/**
	 * @return Returns the s_type.
	 */
	public String getS_type() {
		return s_type;
	}

	/**
	 * @return Returns the sr_code.
	 */
	public String getSr_code() {
		return sr_code;
	}

	/**
	 * @return Returns the sr_value.
	 */
	public String getSr_value() {
		return sr_value;
	}

	/**
	 * @param s_type
	 *            The s_type to set.
	 */
	public void setS_type(String s_type) {
		this.s_type = s_type;
	}

	/**
	 * @param sr_code
	 *            The sr_code to set.
	 */
	public void setSr_code(String sr_code) {
		this.sr_code = sr_code;
	}

	/**
	 * @param sr_value
	 *            The sr_value to set.
	 */
	public void setSr_value(String sr_value) {
		this.sr_value = sr_value;
	}

	/**
	 * Return the select item (common info type) for selection component.
	 * 
	 * @return the collection of select item (common info type).
	 * @throws Exception
	 */
	public Collection getTypelist() throws Exception {
		List<SelectItem> list = new ArrayList<SelectItem>();

		for (CommoninfoType ct : CommoninfoType.values())
			list.add(new SelectItem(ct.name(), ct.getDesc()));

		return list;
	}

	/**
	 * Return the select item (home page) for selection component.
	 * 
	 * @return The select item (home page) for selection component.
	 * @throws Exception
	 */
	public Collection getHomepages() throws Exception {
		List<SelectItem> list = new ArrayList<SelectItem>();
		list.add(new SelectItem("", ""));
		ICommoninfoProcess cprocess = (ICommoninfoProcess) ProcessFactory
				.get(Commoninfo.class);
		Collection homepages = cprocess.list(CommoninfoType.homepage.name());

		if (homepages != null) {
			for (Iterator iterator = homepages.iterator(); iterator.hasNext();) {
				Commoninfo commoninfo = (Commoninfo) iterator.next();
				list.add(new SelectItem(commoninfo.getValue(), commoninfo
						.getCode()));
			}
		}
		return list;
	}
}
